package com.lyc.utils;

import com.lyc.entity.Product;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class CollectionUtil {

    public static boolean notEmpty(Collection collection) {
        return collection != null && collection.size() > 0;
    }

    public static boolean isEmpty(Collection collection) {
        return !notEmpty(collection);
    }

    public static boolean notEmpty(Map map) {
        return map != null && map.size() > 0;
    }

    public static boolean isEmpty(Map map) {
        return !notEmpty(map);
    }

    /**
     * 截取最新商品集合以及最热商品集合生成首页轮播图所需的集合数据
     *
     * @param step 将源集合截取几次
     * @param src  源集合
     * @return 返回List<List<Product>>集合
     */
    public static List<List<Product>> subList(int step, List<Product> src) {
        List<List<Product>> list = null;
        if (notEmpty(src)) {
            list = new ArrayList<>();
            for (int i = 0, j = i; i < src.size(); i += j) {
                if (j + step <= src.size())
                    j += step;
                else
                    j = src.size() - j;
                List<Product> subList = src.subList(i, j);
                list.add(subList);
            }
        }
        return list;
    }
}
